package com.logic.landseaserver.ws;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.enums.BillInputTypeEnum;
import com.logic.landseaserver.domain.LateFeeDetail;
import com.logic.landseaserver.domain.MonthlyBill;
import com.logic.landseaserver.domain.RealBill;
import com.logic.landseaserver.models.latefee.service.LateFeeService;
import com.logic.landseaserver.service.IMonthlyBillService;
import com.logic.landseaserver.service.IRealBillService;
import com.logic.landseaserver.service.utils.ResetQueryUtil;
import com.logic.landseaserver.ws.dto.MonthlyBillDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


@Path("/monthlyBill")
@Api(value="monthlyBill")
public class MonthlyBillRest {
	@Autowired
	private IMonthlyBillService monthlyBillServiceImpl;

	@Autowired
	private IRealBillService realBillServiceImpl;
	
	@Autowired
	private LateFeeService lateFeeService;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new monthlyBill")
	public Response create(@ApiParam(value = "param", required = true) MonthlyBill monthlyBill) {
		CommonResultMap map = new CommonResultMap();
        try {
        	monthlyBill.setInputType(BillInputTypeEnum.BACK.getCode());
        	int id = monthlyBillServiceImpl.insert(monthlyBill);
        	map.setData(monthlyBillServiceImpl.getDTO(id));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("create error.", e);
            map.setResult(e);
        } catch (Exception e) {
            logger.error("create error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}


	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete monthlyBill by set deleted")
    public Response logicDelete(@ApiParam(value = "param", required = true) @QueryParam("monthlyBillId") String id) {
        CommonResultMap map = new CommonResultMap();
        try {
        	monthlyBillServiceImpl.delete(id);
            map.setResult(CommonResult.SUCCESS,"delete monthlyBill");
        } catch (BusinessException e) {
            logger.error("logicDelete error.", e);
            map.setResult(e);
        }  catch (Exception e) {
            logger.error("logicDelete error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "edit monthlyBill ")
    public Response update(@ApiParam(value = "param", required = true) MonthlyBill monthlyBill) {
        CommonResultMap map = new CommonResultMap();
        try {
        	monthlyBillServiceImpl.update(monthlyBill);
            map.setResult(CommonResult.SUCCESS,"edit monthlyBill");
        } catch (BusinessException e) {
            logger.error("update error.", e);
            map.setResult(e);
        }  catch (Exception e) {
            logger.error("update error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @GET
    @Path("/detail/{monthlyBillId}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get monthlyBill by monthlyBill id")
    public Response retrieveDTO(
            @ApiParam(value = "param", required = true) @PathParam("monthlyBillId") Integer monthlyBillId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(monthlyBillServiceImpl.getDTO(monthlyBillId));
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("retrieveDTO error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


	@POST
	@Path("/detail/list")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list monthlyBills")
	public Response listDTO(@ApiParam(value = "param", required = true) QueryUtil queryUtil){
        CommonResultMap map = new CommonResultMap();
        List<MonthlyBillDTO> monthlyBillList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            ResetQueryUtil.reset(queryUtil);
            monthlyBillList = monthlyBillServiceImpl.queryDTOWithNoCancel(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(monthlyBillList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("listDTO error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	@POST
	@Path("/save")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new monthlyBill")
	public Response save(@ApiParam(value = "param", required = true) MonthlyBill monthlyBill) {
		CommonResultMap map = new CommonResultMap();
        try {
        	Integer id = null;
        	if (monthlyBill.getExpenseType().indexOf("_") == -1) {
        		RealBill realBill = new RealBill();
        		realBill.setBillPrice(monthlyBill.getBillPrice());
        		realBill.setComment(monthlyBill.getComment());
        		realBill.setContractId(monthlyBill.getContractId());
        		realBill.setExpenseType(monthlyBill.getExpenseType());
        		realBill.setInputType(BillInputTypeEnum.BACK.getCode());
        		id = realBillServiceImpl.insert(realBill);
        		map.setData(realBillServiceImpl.getDTO(id));
        	} else {
        		monthlyBill.setInputType(BillInputTypeEnum.BACK.getCode());
        		id = monthlyBillServiceImpl.insert(monthlyBill);
        		map.setData(monthlyBillServiceImpl.getDTO(id));
        	}
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("save error.", e);
            map.setResult(e);
        } catch (Exception e) {
            logger.error("save error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	    @POST
	    @Path("/createMonthEleBill")
	    @Produces(MediaType.APPLICATION_JSON)
	    @Consumes(MediaType.APPLICATION_JSON)
	    @ApiOperation(value = "测试生成电账单", position = 8)
	    public Response createMonthEleBill() {
	        CommonResultMap map = new CommonResultMap();
	        Map<String, Object> result = new HashMap<String, Object>();
	        try {
	            monthlyBillServiceImpl.createEleMonthlyBill();
	            result.put("STATUS", 0);
	            map.setData(result);
	        } catch (Exception e) {
	            logger.error("createMonthEleBill error.", e);
	            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
	        }
	        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	    }

	    @POST
	    @Path("/latefeeList")
	    @Produces(MediaType.APPLICATION_JSON)
	    @Consumes(MediaType.APPLICATION_JSON)
	    @ApiOperation(value = "滞纳金详情列表")
	    public Response latefeeList(@ApiParam(value = "param", required = false) LateFeeDetail lateFeeDetail)
	    {
	        CommonResultMap map = new CommonResultMap();
	        List<LateFeeDetail> list = new ArrayList<LateFeeDetail>();
	        try
	        {
	            list = lateFeeService.latefeeList(lateFeeDetail.getBillId());
	            map.setData(list);
	            map.setResult(CommonResult.SUCCESS);
	        }
	        catch (Exception e)
	        {
               logger.error("latefeeList error.", e);
               map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
	        }
	        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	    }
}
